import React from "react";
import {Button,  Divider,Menu,  Modal} from 'antd/lib';
import SealTable from "@/components/contract/SealTable";
import SealTimeLine from "@/components/contract/SealTimeLine";
import SealAttache from "@/components/contract/SealAttache";
import ContractSeal from "@/services/contractseal";
import notification from "@/services/notification";
import {get} from "lodash";
import CheckDialog from "@/components/workflow/CheckDialog";
import {currentUser} from "@/services/auth";
import Workflow from "@/services/workflow";
import moment from "moment";
import WarningOutlined from "@ant-design/icons/WarningOutlined";
import Contracts from "@/services/contracts";
import GlobalFunctions from '@/store/globalFunctions';
import {createBrowserHistory} from 'history';
import { inject, observer } from 'mobx-react';
const customHistory = createBrowserHistory();
const { confirm } = Modal;

class SealToolbar extends React.Component {
  state = {
    loading: true,
    isnew: true,
    openpage:"jbxx",
  };
  componentWillMount() {
    this.refreshData(this.props.id);
    this.setState({
      loading: false,
    });
  }
  refreshData(id = 0) {
    this.props.contractSealStore.initStore(id);
}
  handleadd = () => {
    this.refreshData(0);
  };
  handleSave = () => {

    if(this.props.contractSealStore.contractSealModel.status ===2 && this.props.sealtype==="0"){
      notification.error("已审批，不能保存！");
      return;
    }
    this.props.contractSealStore.dataSave();
  };
  handleDelete = () => {
    if(this.props.contractSealStore.contractSealModel.status !==0){
      notification.error("不是草稿状态，不能删除！");
      return;
    }
    confirm({
      title: '确定删除合同用印申请单?',
      icon: <WarningOutlined />,
      content: this.props.contractSealStore.contractSealModel.name,
      okText: '确定',
      okType: 'danger',
      cancelText: '取消',
      onOk: () => {
        this.props.contractSealStore.dataDelete();
        this.refreshData(0);
      },
    });
  };
  handlebackpage= () => {
    customHistory.goBack();
  };
  tabsClick = e => {
    this.setState({
      openpage: e.key,
    });
  };
  handleCheck = () => {
    this.props.contractSealStore.billCheck(currentUser.id);
  };
  render() {
    if (this.state.loading) return null;
    return(
      <div>
        <div style={{ marginBottom: 16 }}>
        <Button type="primary" style={{margin:"10px"}} onClick={() => this.handlebackpage()}>
            <i className="fa fa-reply m-r-5" />
            返回
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleadd()}>
            <i className="fa fa-plus m-r-5" />
            新增
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleSave()} disabled ={!this.props.contractSealStore.contractSealModel.isedit && this.props.sealtype==="0" }>
            <i className="fa fa-save m-r-5" />
            保存
          </Button>
          <Divider type="vertical" />
          <Button type="primary" onClick={() => this.handleDelete()} disabled ={this.props.contractSealStore.contractSealModel.status !==0}>
            <i className="fa fa-trash m-r-5" />
            删除
          </Button>
          <Divider type="vertical" />
          { (this.props.contractSealStore.contractSealModel.status!=2 || currentUser.hasPermission("admin")) &&(<Button type="primary" onClick={() => this.handleCheck()}>
            <i className="fa fa-check m-r-5" />
            {this.props.contractSealStore.contractSealModel.status===0 ? "提交":"审核"}
          </Button>)}
        </div>
        <div>
        <Menu onClick={this.tabsClick} selectedKeys={this.state.openpage} mode="horizontal">
        <Menu.Item key = "jbxx">基本信息</Menu.Item>
        <Menu.Item key="zlsc">资料上传</Menu.Item>
        <Menu.Item key = "sjx">时间线</Menu.Item>
        </Menu>
        {this.state.openpage==="jbxx" &&
          (<SealTable sealtype={this.props.sealtype}  id={this.props.id} />)}
        {this.state.openpage==="zlsc" &&
          (<SealAttache id={this.props.contractSealStore.contractSealModel.id}
            isedit={this.props.contractSealStore.contractSealModel.isedit} colcontrol={this.props.contractSealStore.contractSealModel.colcontrol}
            allow_col={this.props.contractSealStore.contractSealModel.allow_col} status={this.props.contractSealStore.contractSealModel.status}
          />)}
        {this.state.openpage==="sjx" &&
          (<SealTimeLine  id={this.props.contractSealStore.contractSealModel.id}/>)}
        </div>
      </div>
    );
  };
}

export default inject('contractSealStore')(observer(SealToolbar));